﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//new
using DataRecv.db;
using DataRecv.models;
using DataRecv.enums;

namespace DataRecv
{
    public partial class Form_brt : Form
    {
        private bool start = false;
        private const string c_TableName = "BRT_t";
        private const string c_Columns = "hphm";
        public Form_brt()
        {
            InitializeComponent();
        }

        private void Form_brt_Load(object sender, EventArgs e)
        {
            try
            {
                if (G.BRTs != null)
                {
                    dataGridView1.Rows.Clear();
                    foreach(string brt in G.BRTs)
                    {
                        dataGridView1.Rows.Add(brt);
                    }
                }

                this.checkBox_BrtEnable.Checked = G.SystemParam.BRTEnable;
            }
            catch{ }
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox_brt.Text != "")
                {
                    string[] ss = new string[1] { textBox_brt.Text };
                    bool flag = DBHelper.GetInstance().Add(c_TableName, c_Columns, ss);
                    if (flag == true)
                    {
                        if (G.BRTs != null)
                        {
                            G.BRTs.Add(textBox_brt.Text);
                        }
                        this.dataGridView1.Rows.Add(textBox_brt.Text);
                        MessageBox.Show (textBox_brt.Text + "添加成功");
                    }
                    else
                    {
                        MessageBox.Show ( textBox_brt.Text + "添加失败");
                    }
                }
            }
            catch { }
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView1.SelectedRows.Count < 1)
                {
                    MessageBox.Show("未选择要删除的记录");
                }
                else
                {
                    string hphm = this.dataGridView1.SelectedRows[0].Cells["hphm"].Value.ToString();

                    if (DBHelper.GetInstance().Delete(c_TableName, "hphm='" + hphm + "'") == true)
                    {
                        if (G.BRTs != null)
                        {
                            G.BRTs.Remove(hphm);
                        }
                        this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
                        MessageBox.Show("删除成功");
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                }
            }
            catch { }
        }

        private void checkBox_BrtEnable_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (start == true)
                {
                    bool flag = G.SystemParam.Save(Enum_SystemParamName.BRTEnable, this.checkBox_BrtEnable.Checked);
                    if (flag == true)
                    {
                        G.SystemParam.BRTEnable = this.checkBox_BrtEnable.Checked;
                        MessageBox.Show("功能已" + (this.checkBox_BrtEnable.Checked ? "启用" : "停用"));
                    }
                }
            }
            catch { }
        }

        private void Form_brt_Shown(object sender, EventArgs e)
        {
            start = true;
        }
    }
}
